/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package main;

import java.awt.Color;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import javax.swing.JScrollBar;
import javax.swing.JTextArea;

import jdbc.DataCheck;
import bean.LinkInfo;

/**
 *
 * @author 樂淵
 */
public class MainFrame extends javax.swing.JFrame {

	private static final long serialVersionUID = 1L;
	
	//保存连接信息
	Properties p = new Properties();
    public MainFrame() {
        initComponents();
        setResizable(false);
        setTitle("数据比对");
        setBounds(500, 20, 812, 730);
        
        InputStream is;
		try {
			is = MainFrame.class.getResourceAsStream("/prop/LinkInfo.properties");
			if(is==null)
				return;
			p.load(is);
			
			url1.setText(p.getProperty("url1"));
			url2.setText(p.getProperty("url2"));
			username1.setText(p.getProperty("username1"));
			username2.setText(p.getProperty("username2"));
			password1.setText(p.getProperty("password1"));
			password2.setText(p.getProperty("password2"));
			driver1.setText(p.getProperty("driver1"));
			driver2.setText(p.getProperty("driver2"));
			sql1.setText(p.getProperty("sql1"));
			sql2.setText(p.getProperty("sql2"));
			
		} catch (IOException e) {
			showErrorLogs(e.getMessage(),e.getStackTrace());
		}
        
    }

    /**
     */
    private void initComponents() {

        MainPanel = new javax.swing.JPanel();
        MainPanel.setBackground(new Color(232,242,254));
        jLabel1 = new javax.swing.JLabel();
        url2 = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        username2 = new javax.swing.JTextField();
        password2 = new javax.swing.JTextField();
        driver2 = new javax.swing.JTextField();
        url1 = new javax.swing.JTextField();
        username1 = new javax.swing.JTextField();
        password1 = new javax.swing.JTextField();
        driver1 = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        scrollPane1 = new javax.swing.JScrollPane();
        message1 = new javax.swing.JTextArea();
        jScrollPane2 = new javax.swing.JScrollPane();
        scrollPane2 = new javax.swing.JScrollPane();
        message2 = new javax.swing.JTextArea();
        jScrollPane3 = new javax.swing.JScrollPane();
        sql1 = new javax.swing.JTextArea();
        jScrollPane4 = new javax.swing.JScrollPane();
        sql2 = new javax.swing.JTextArea();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("URL");

        jLabel2.setText("URL");

        jLabel3.setText("用户名");

        jLabel4.setText("用户名");

        jLabel5.setText("密码");

        jLabel6.setText("密码");

        jLabel7.setText("驱动");

        jLabel8.setText("驱动");

        jButton1.setText("开始");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                start(evt);
            }
        });

        jButton2.setText("帮助");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                getTips(evt);
            }
        });

        message1.setEditable(false);
        message1.setColumns(20);
        message1.setRows(5);
        scrollPane1.setViewportView(message1);

        jScrollPane1.setViewportView(scrollPane1);

        message2.setEditable(false);
        message2.setColumns(20);
        message2.setRows(10);
        scrollPane2.setViewportView(message2);

        jScrollPane2.setViewportView(scrollPane2);

        sql1.setColumns(20);
        sql1.setRows(5);
        jScrollPane3.setViewportView(sql1);

        sql2.setColumns(20);
        sql2.setRows(5);
        jScrollPane4.setViewportView(sql2);

        jLabel9.setText("SQL：");

        jLabel10.setText("SQL：");

        javax.swing.GroupLayout MainPanelLayout = new javax.swing.GroupLayout(MainPanel);
        MainPanel.setLayout(MainPanelLayout);
        MainPanelLayout.setHorizontalGroup(
            MainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, MainPanelLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jButton2)
                .addGap(18, 18, 18)
                .addComponent(jButton1)
                .addGap(54, 54, 54))
            .addGroup(MainPanelLayout.createSequentialGroup()
                .addGap(17, 17, 17)
                .addGroup(MainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(MainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addGroup(MainPanelLayout.createSequentialGroup()
                            .addComponent(jLabel9)
                            .addGap(41, 41, 365))
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, MainPanelLayout.createSequentialGroup()
                            .addGroup(MainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addGroup(javax.swing.GroupLayout.Alignment.LEADING, MainPanelLayout.createSequentialGroup()
                                    .addComponent(jLabel5)
                                    .addGap(18, 18, 18)
                                    .addComponent(password1, javax.swing.GroupLayout.PREFERRED_SIZE, 328, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGroup(javax.swing.GroupLayout.Alignment.LEADING, MainPanelLayout.createSequentialGroup()
                                    .addComponent(jLabel3)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addGroup(MainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(url1, javax.swing.GroupLayout.PREFERRED_SIZE, 328, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(username1, javax.swing.GroupLayout.PREFERRED_SIZE, 328, javax.swing.GroupLayout.PREFERRED_SIZE))))
                            .addGap(29, 29, 29)))
                    .addGroup(MainPanelLayout.createSequentialGroup()
                        .addGroup(MainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 376, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel1)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 376, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(MainPanelLayout.createSequentialGroup()
                                .addComponent(jLabel8)
                                .addGap(18, 18, 18)
                                .addComponent(driver1, javax.swing.GroupLayout.PREFERRED_SIZE, 328, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(25, 25, 25)))
                .addGroup(MainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(MainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addGroup(MainPanelLayout.createSequentialGroup()
                            .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(url2, javax.swing.GroupLayout.PREFERRED_SIZE, 328, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(MainPanelLayout.createSequentialGroup()
                            .addComponent(jLabel6)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(password2, javax.swing.GroupLayout.PREFERRED_SIZE, 328, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(MainPanelLayout.createSequentialGroup()
                            .addComponent(jLabel4)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(username2, javax.swing.GroupLayout.PREFERRED_SIZE, 328, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(MainPanelLayout.createSequentialGroup()
                            .addComponent(jLabel7)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(driver2, javax.swing.GroupLayout.PREFERRED_SIZE, 328, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addComponent(jScrollPane2)
                        .addComponent(jScrollPane4))
                    .addComponent(jLabel10))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        MainPanelLayout.setVerticalGroup(
            MainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(MainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(MainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2))
                .addGap(5, 5, 5)
                .addGroup(MainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(url2, javax.swing.GroupLayout.PREFERRED_SIZE,javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(MainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel1)
                        .addComponent(jLabel2)
                        .addComponent(url1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(MainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jLabel4)
                    .addComponent(username2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(username1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(MainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(MainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel5)
                        .addComponent(jLabel6)
                        .addComponent(password1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(password2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(MainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8)
                    .addComponent(jLabel7)
                    .addComponent(driver2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(driver1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(5, 5, 5)
                .addGroup(MainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(jLabel10))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(MainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(MainPanelLayout.createSequentialGroup()
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, 0)
                        .addGroup(MainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane1)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 379, Short.MAX_VALUE)))
                    .addGroup(MainPanelLayout.createSequentialGroup()
                        .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(MainPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 808, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(MainPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }

    
    /**显示提示
     * */
    private void getTips(java.awt.event.ActionEvent evt) {
        new Tips().showTips();
        return;
    }

    private void start(java.awt.event.ActionEvent evt) {
    
    	message1.setText("");
    	message2.setText("");
       String url1Str =  this.url1.getText();
       String username1Str =  this.username1.getText();
       String password1Str =  this.password1.getText();
       String driver1Str =  this.driver1.getText();
       String sql1Str = this.sql1.getText();
       
       String url2Str =  this.url2.getText();
       String username2Str =  this.username2.getText();
       String password2Str =  this.password2.getText();
       String driver2Str =  this.driver2.getText();
       String sql2Str = this.sql2.getText();
       
       if("".equals(url1Str) || "".equals(username1Str)|| "".equals(password1Str) || "".equals(driver1Str) || "".equals(sql1Str)){
           JScrollBar jb = scrollPane1.getVerticalScrollBar();
           refreshMessage(message1,jb,"连接信息或sql不能为空！\n");
       }else if("".equals(url2Str) || "".equals(username2Str) || "".equals(password2Str) ||"".equals(driver2Str) || "".equals(sql2Str)){
            JScrollBar jb = scrollPane2.getVerticalScrollBar();
           refreshMessage(message2,jb,"连接信息或sql不能为空！\n");
       
       }else{
       
		LinkInfo li = new LinkInfo();
		li.setUrl1(url1Str);
		li.setUsername1(username1Str);
		li.setPassword1(password1Str);
		li.setDriver1(driver1Str);
		li.setSql1(sql1Str);
		   
		li.setUrl2(url2Str);
		li.setUsername2(username2Str);
		li.setPassword2(password2Str);
		li.setDriver2(driver2Str);
		li.setSql2(sql2Str);
		
		p.setProperty("url1",url1Str);
		p.setProperty("url2",url2Str);
		p.setProperty("username1",username1Str);
		p.setProperty("username2",username2Str);
		p.setProperty("password1",password1Str);
		p.setProperty("password2",password2Str);
		p.setProperty("driver1",driver1Str);
		p.setProperty("driver2",driver2Str);
		p.setProperty("sql1",sql1Str);
		p.setProperty("sql2",sql2Str);
		
       
		DataCheck dc = new DataCheck(li,this);
		dc.queryData();
		dc.check();
      }
       
    }

    /**@param jta
     * @param jb
     * @param  text
     * 自动更新消息文本框
     */
    public void refreshMessage(JTextArea jta,JScrollBar jb, String text){
       jta.append(text+"\n"); 
       if("连接信息或sql不能为空！\n".equals(text)){
           return;
       }
    }
    
    /**显示错误日志
     * @param string 
     * */
    public void showErrorLogs(String message, StackTraceElement[] text){
    	new ErrorLog(message,text);
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        
        final MainFrame mf = new MainFrame();
        mf.setBackground(new Color(156,183,12));
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                mf.setVisible(true);
            }
        });
    }

    private javax.swing.JPanel MainPanel;
    private javax.swing.JTextField driver1;
    private javax.swing.JTextField driver2;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JTextArea sql1;
    private javax.swing.JTextArea sql2;
    public javax.swing.JTextArea message1;
    public javax.swing.JTextArea message2;
    private javax.swing.JTextField password1;
    private javax.swing.JTextField password2;
    private javax.swing.JScrollPane scrollPane1;
    private javax.swing.JScrollPane scrollPane2;
    private javax.swing.JTextField url1;
    private javax.swing.JTextField url2;
    private javax.swing.JTextField username1;
    private javax.swing.JTextField username2;
}
